package filmSystem;

import java.sql.*;
import java.util.Scanner;
public class FilmSystemTest {
    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);
        Connection connection = DriverManager.getConnection(
                "jdbc:MYSQL://localhost:3306/film_sys"+
                        "?serverTimezone=GMT&useSSL=false",
                "root", "A7btngqxA7btngqx");
        while (true) {
            System.out.println("1：维护\n2：查询");

            int a = scanner.nextInt();
            switch (a) {
                case 1: {
                    while (true) {
                        System.out.println("1:增加电影\n2：删除电影\n3:修改电影\n4:退出");
                        System.out.println("输入你的选择");

                        int b = scanner.nextInt();
                        switch (b) {
                            case 1: {
                                System.out.println("输入电影名称 类型 评分 演员 年代");
                                String name = scanner.next();
                                String kind = scanner.next();
                                double score = scanner.nextDouble();
                                String character = scanner.next();
                                int age = scanner.nextInt();
                                String sql = "INSERT INTO `movie`.`movie` (`name`, `kind`, `score`, `character`, `age`) VALUES (?, ?, ?, ?, ?);";
                                PreparedStatement statement = connection.prepareStatement(sql);
                                statement.setString(1, name);
                                statement.setString(2, kind);
                                statement.setDouble(3, score);
                                statement.setString(4, character);
                                statement.setInt(5, age);
                                int result = statement.executeUpdate();
                                statement.close();
                                continue;
                            }
                            case 2: {
                                System.out.println("输入电影名");
                                String name = scanner.next();

                                String sql = "delete from film_sys where name=?";
                                PreparedStatement statement = connection.prepareStatement(sql);
                                statement.setString(1, name);
                                int result = statement.executeUpdate();
                                statement.close();
                                continue;
                            }
                            case 3: {
                                System.out.println("输入电影名称 类型 评分 演员 年代");
                                String name = scanner.next();
                                String kind = scanner.next();
                                double score = scanner.nextDouble();
                                String character = scanner.next();
                                int age = scanner.nextInt();
                                String sql = "UPDATE `film_sys`.`movie` SET `kind` = ?, `score` = ?, `character` = ?, `age` = ? WHERE (`name` = ?)  ";
                                PreparedStatement statement = connection.prepareStatement(sql);
                                statement.setString(1, kind);
                                statement.setDouble(2, score);
                                statement.setString(3, character);
                                statement.setInt(4, age);
                                statement.setString(5, name);
                                int result = statement.executeUpdate();
                                statement.close();
                                continue;
                            }
                            case 4: {
                                break;
                            }
                            default: {
                                System.out.println("重新输入");
                                continue;
                            }
                        }
                        break;
                    }
                }
                case 2: {
                    ResultSet resultSet = null;
                    System.out.println("选择查询方式\n1：年代\n2：类型\n3：名字\n4：演员");
                    int d=0;
                    while (d==0){
                        int c= scanner.nextInt();
                        switch (c){
                            case 1:{
                                System.out.println("输入电影年代");
                                int age= scanner.nextInt();
                                String sql="select *from movie where (age=?)";
                                PreparedStatement statement=connection.prepareStatement(sql);
                                statement.setInt(1,age);
                                resultSet= statement.executeQuery();
                                d++;
                                break;
                            }
                            case 2:{
                                System.out.println("喜剧片 动作片 恐怖片 ");
                                System.out.println("输入电影类型");
                                String kind= scanner.next();
                                String sql="select *from movie where (kind=?)";
                                PreparedStatement statement=connection.prepareStatement(sql);
                                statement.setString(1,kind);
                                resultSet= statement.executeQuery();
                                d++;
                                break;
                            }
                            case 3:{
                                System.out.println("输入电影名字");
                                String name= scanner.next();
                                String sql="select *from movie where (name=?)";
                                PreparedStatement statement=connection.prepareStatement(sql);
                                statement.setString(1,name);
                                resultSet= statement.executeQuery();
                                d++;
                                break;
                            }
                            case 4:{
                                System.out.println("输入电影演员");
                                String character= scanner.next();
                                String sql="select *from movie where (character=?)";
                                PreparedStatement statement=connection.prepareStatement(sql);
                                statement.setString(1,character);
                                resultSet= statement.executeQuery();
                                d++;
                                break;
                            }
                            default:{
                                System.out.println("输入错误重新输入");
                                break;
                            }
                        }
                    }
                    while(resultSet.next()){
                        String name1= resultSet.getString("name");
                        String character1= resultSet.getString("character");
                        double score1= resultSet.getInt("score");
                        int  age1= resultSet.getInt("age");
                        String kind1= resultSet.getString("kind");
                        System.out.println("名字："+name1+"  年龄："+age1+"  类型："+kind1+"  评分"+score1+"  演员"+character1);}
                    break;
                }

                default: {
                    System.out.println("输入错误重新选择");
                    break;
                }
            }
        }
    }
}